﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using AviFile;



namespace Puri_on_Capture
{
    public partial class MainForm : Form
    {

        private Boolean running;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            BitmapImage.MPEGFunctions a = new BitmapImage.MPEGFunctions();
            
        }
        public Bitmap GetDesktopImage(Rectangle rect)
        {

            Graphics graphics;
            Bitmap bitmap;

            bitmap = new Bitmap(rect.Width, rect.Height);
            graphics = Graphics.FromImage(bitmap);
            graphics.CopyFromScreen(0, 0, rect.Left, rect.Top, new Size(rect.Width, rect.Height));
            return bitmap;
        }

        private Avi.AVICOMPRESSOPTIONS init_codec()
        {
            
            Avi.AVICOMPRESSOPTIONS com = new Avi.AVICOMPRESSOPTIONS();
            //com.cbFormat = 0;
            //com.cbParms = 0;
            //com.dwBytesPerSecond = 0;
            //com.dwFlags = 8;
            //com.dwInterleaveEvery = 4;
            //com.dwKeyFrameEvery = 0;
            //com.dwQuality = 7500;
            //com.fccHandler = 1668707181;
            //com.fccType = 0;
            //com.lpFormat = (IntPtr)(0);
            //com.lpParms = (IntPtr)0;

            com.cbFormat = 0;
            com.cbParms = 0;
            com.dwBytesPerSecond = 0;
            com.dwFlags = 8;
            com.dwInterleaveEvery = 4;
            com.dwKeyFrameEvery = 0;
            com.dwQuality = 7500;
            com.fccHandler = 1935959654;
            com.fccType = 0;
            com.lpFormat = (IntPtr)(0);
            com.lpParms = (IntPtr)0;

            return com;
        }

        public String setOutputFile()
        {
            // Use SaveFile dialog to specify output file name
            SaveFileDialog dlg = new SaveFileDialog();
            if (dlg.ShowDialog() != DialogResult.OK)
                return "";
            return dlg.FileName;
        }

        private void StartCapture_Click(object sender, EventArgs e)
        {

            
            StopCapture.Enabled = true;
            StartCapture.Enabled = false;
            running = true;


            Rectangle rec = new Rectangle(0, 0, 800, 600);          
            String filename = "cap";
            

            /*
            MPEG.myMpeg my = new MPEG.myMpeg(filename + ".avi");

            Boolean loop = true;
            DateTime n = DateTime.Now;

            while (loop)
            {
                if (DateTime.Now > n.AddSeconds(30)) { loop = false; }

                
                my.addFrame(GetDesktopImage(rec),10);
                Application.DoEvents();
            }
            my.close();
            */

            Bitmap bmp = GetDesktopImage(rec);
            int fcnt = 0;
            while (running)
            {

                



                
                AviManager aviManager = new AviManager(filename + fcnt.ToString() + ".avi", false);
                

                //코덱 선택 가능
                //VideoStream videoStream = aviManager.AddVideoStream(true, 30, bmp);

                
                
                //코덱 임의 설정
                VideoStream videoStream = aviManager.AddVideoStream(init_codec(), 24, bmp);


                Bitmap bitmap;

                Boolean loop = true;
                DateTime n = DateTime.Now;

                while (loop)
                {
                    if (DateTime.Now > n.AddSeconds(10)) { loop = false; }

                    bitmap = GetDesktopImage(rec);
                    videoStream.AddFrame(bitmap);
                    Application.DoEvents();
                }

                videoStream.GetFrameClose();

                aviManager.Close();
                fcnt++;
                
            }
            
            StopCapture.Enabled = false;
            StartCapture.Enabled = true;

            MessageBox.Show("done");
        }

        private void StopCapture_Click(object sender, EventArgs e)
        {
            StopCapture.Enabled = false;
            StartCapture.Enabled = true;
            running = false;
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            running = false;
        }
    }
}